package com.agd.quick.states;
//l
import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;

import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import lib.elementgame.GameSprite;
import lib.elementgame.GameText;
import lib.engine.Anchor;
import lib.engine.GameEngine;
import lib.engine.GameState;

public class StateLeaderBoard extends GameState {
	
	private static GameSprite backgroundFloor;
	private static GameSprite btnFB;
	private static GameSprite btnTwitter;
	
	private GameText highscore;
	private GameText score1;
	private GameText score2;
	private GameText score3;
	private GameText score4;
	private GameText score5;
	
	private int scr1 = 0; 
	private int scr2 = 0;
	private int scr3 = 0;
	private int scr4 = 0;
	private int scr5 = 0;
	
	private boolean keluar;


	public StateLeaderBoard(GameEngine engine) {
		super(engine);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
			ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY) {
		// TODO Auto-generated method stub
		if(pSceneTouchEvent.isActionDown()){
			if(pTouchArea == btnFB){
				String url = "https://www.facebook.com/amikomgamedev";
				Intent start = new Intent(Intent.ACTION_VIEW);
				start.setData(Uri.parse(url));
				engine.startActivity(start);
			}
			else if(pTouchArea == btnTwitter){
				String url = "https://twitter.com/amikomgamedev";
				Intent start = new Intent(Intent.ACTION_VIEW);
				start.setData(Uri.parse(url));
				engine.startActivity(start);
			}
		}
		return false;
	}

	@Override
	public void initComponent() {
		// TODO Auto-generated method stub
		backgroundFloor = new GameSprite(LDR_SPRITE_BACKGROUND2, engine);
		btnFB = new GameSprite(LDR_SPRITE_BTNFB, engine);
		btnTwitter = new GameSprite(LDR_SPRITE_BTNTWITTER, engine);
		highscore = new GameText("", 50, engine.getFont(FONT_CIRCULAT_20_BLACK), engine);
		score1 = new GameText("", 150, engine.getFont(FONT_CIRCULAT_20_BLACK), engine);
		score2 = new GameText("", 150, engine.getFont(FONT_CIRCULAT_20_BLACK), engine);
		score3 = new GameText("", 150, engine.getFont(FONT_CIRCULAT_20_BLACK), engine);
		score4 = new GameText("", 150, engine.getFont(FONT_CIRCULAT_20_BLACK), engine);
		score5 = new GameText("", 150, engine.getFont(FONT_CIRCULAT_20_BLACK), engine);
	}

	@Override
	protected void init() {
		// TODO Auto-generated method stub
		backgroundFloor.setWidth(800);
		backgroundFloor.setHeight(480);
		keluar = false;
		String strScore = engine.getDatabase().getData(TABLE_SCORE1, 0, 1);
		String strScore2 = engine.getDatabase().getData(TABLE_SCORE2, 0, 1);
		String strScore3 = engine.getDatabase().getData(TABLE_SCORE3, 0, 1);
		String strScore4 = engine.getDatabase().getData(TABLE_SCORE4, 0, 1);
		String strScore5 = engine.getDatabase().getData(TABLE_SCORE5, 0, 1);
		
		scr1 = Integer.parseInt(strScore);
		scr2 = Integer.parseInt(strScore2);
		scr3 = Integer.parseInt(strScore3);
		scr4 = Integer.parseInt(strScore4);
		scr5 = Integer.parseInt(strScore5);
		
//		if(scr1 == 0){
//			scr1 = tempScore;
//		}
//		if(tempScore > scr1 && tempScore > scr3 && tempScore > scr4 && tempScore > scr5){
//			scr5 = scr4;
//			scr4 = scr3;
//			scr3 = scr2;
//			scr2 = scr1;
//			scr1 = tempScore;
//		}
//		if(tempScore < scr1 && tempScore > scr3 && tempScore > scr4 && tempScore > scr5){
//			scr5 = scr4;
//			scr4 = scr3;
//			scr3 = scr2;
//			scr2 = tempScore;
//			engine.getDatabase().updateData(TABLE_SCORE2, new int[]{1}, new String[]{"" + scr2}, "WHERE id_score = 0");
//		}
//		if(tempScore < scr1 && tempScore < scr2 && tempScore > scr4 && tempScore > scr5){
//			scr5 = scr4;
//			scr4 = scr3;
//			scr3 = tempScore;
//		}
//		if(tempScore < scr1 && tempScore < scr2 && tempScore < scr3 && tempScore > scr5){
//			scr5 = scr4;
//			scr4 = tempScore;
//		}
//		if(tempScore < scr1 && tempScore < scr2 && tempScore < scr3 && tempScore < scr4){
//			scr5 = tempScore;
//		}
		highscore.setText("High Score");
		score1.setText("#1------- 		\t\t" + scr1);
		score2.setText("#2------- 		\t\t" + scr2);
		score3.setText("#3------- 		\t\t" + scr3);
		score4.setText("#4------- 		\t\t" + scr4);
		score5.setText("#5------- 		\t\t" + scr5);
	}

	@Override
	protected void attach() {
		// TODO Auto-generated method stub
		engine.scene.attachChild(backgroundFloor);
		engine.scene.attachChild(btnFB);
		engine.scene.attachChild(btnTwitter);
		engine.scene.attachChild(highscore);
		
		engine.scene.attachChild(score1);
		engine.scene.attachChild(score2);
		engine.scene.attachChild(score3);
		engine.scene.attachChild(score4);
		engine.scene.attachChild(score5);
		
	}

	@Override
	protected void detach() {
		// TODO Auto-generated method stub
		backgroundFloor.detachSelf();
		btnFB.detachSelf();
		btnTwitter.detachSelf();
		highscore.detachSelf();
		score1.detachSelf();
		score2.detachSelf();
		score3.detachSelf();
		score4.detachSelf();
		score5.detachSelf();
	}

	@Override
	protected void setPosition() {
		// TODO Auto-generated method stub
		highscore.setPosition(5, 30, Anchor.TOP_CENTER);
		btnFB.setPosition(-160, -10, Anchor.BOTTOM_RIGHT);
		btnTwitter.setPosition(-70, -10, Anchor.BOTTOM_RIGHT);
		score1.setPosition(250, 100);
		score2.setPosition(250, 165);
		score3.setPosition(250, 225);
		score4.setPosition(250, 280);
		score5.setPosition(250, 340);
	}

	@Override
	protected void registerTouch() {
		// TODO Auto-generated method stub
		engine.scene.registerTouchArea(btnFB);
		engine.scene.registerTouchArea(btnTwitter);
	}

	@Override
	protected void unregisterTouch() {
		// TODO Auto-generated method stub
		engine.unregisterSceneTouch(btnFB);
		engine.unregisterSceneTouch(btnTwitter);
	}

	@Override
	protected void onUpdate() {
		// TODO Auto-generated method stub
		if(keluar){
			exitState(MainMenuState);
		}
	}

	@Override
	protected void onPaused() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onResumed() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			keluar = true;
		}
	}

	@Override
	public void initLoading() {
		// TODO Auto-generated method stub
		
	}

}
